<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: index.php,v 1.8 2006/03/10 23:12:03 nathan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("../config.php");
$skip_header	= 1;
require($libdir . "init." . $phpEx);
require($libdir . "chat_functs." . $phpEx);
require($libdir . "admin." . $phpEx);
require($libdir . "rooms." . $phpEx);

$op	= strtolower($_REQUEST['op']);

$template	= new Template($config['TPL_DIR']);
if ($op == '') {
	$sql			= "SELECT COUNT(user_id) AS prefs_count FROM {$dbprefix}prefs";
	$res			= $db->sql_query($sql);
	$row			= $db->sql_fetchrow($res);
	$stats['prefs']		= $row['prefs_count'];
	
	$sql			= "SELECT COUNT(room_id) AS room_count FROM {$dbprefix}rooms";
	$res			= $db->sql_query($sql);
	$row			= $db->sql_fetchrow($res);
	$stats['rooms']		= $row['room_count'];
	
	$sql			= "SELECT COUNT(event_id) AS event_count FROM {$dbprefix}events";
	$res			= $db->sql_query($sql);
	$row			= $db->sql_fetchrow($res);
	$stats['events']	= $row['event_count'];
	
	$sql			= "SELECT COUNT(alias_id) AS alias_count FROM {$dbprefix}aliases";
	$res			= $db->sql_query($sql);
	$row			= $db->sql_fetchrow($res);
	$stats['aliases']	= $row['alias_count'];
	
	$sql			= "SELECT COUNT(session_id) AS session_count FROM {$dbprefix}sessions";
	$res			= $db->sql_query($sql);
	$row			= $db->sql_fetchrow($res);
	$stats['sessions']	= $row['session_count'];
	
	foreach ($stats as $stat => $stat_value) {
		$stat_name	= ucfirst($stat);
		//$stat_value	= $stats[$stat];
	
		print("$stat_name is $stat_value<br />");
	}
} else if (($op == 'manage_rooms') || ($op == 'del_room') || ($op == 'new_room')) {
	if ($op == 'del_room') {
		$sql	= "SELECT * FROM {$dbprefix}rooms WHERE room_id={$_REQUEST['rid']}";
		$res	= $db->sql_query($sql);
		$row	= $db->sql_fetchrow($res);
	
		$sql	= "DELETE FROM {$dbprefix}events WHERE room_id_to={$_REQUEST['rid']}";
		$res	= $db->sql_query($sql);
		$events	= $db->sql_numrows($res);
		if ($events == '') {
			$events	= 0;
		}

		$sql	= "DELETE FROM {$dbprefix}rooms WHERE room_id={$_REQUEST['rid']}";
		$res	= $db->sql_query($sql);
	
		$notice	= "Deleted <b>" . $row['room_name'] . "</b> ($events events purged).";
	} else if ($op == 'new_room') {
		if ($_REQUEST['room_name'] != '') {
			$sql	= "INSERT INTO {$dbprefix}rooms (room_name, room_group) VALUES ('{$_REQUEST['room_name']}', {$_REQUEST['gid']})";
			$res	= $db->sql_query($sql);
	
			$notice	= "Added new room '{$_REQUEST['room_name']}'.";
		} else {
			$notice	= "<b>ERROR:</b> Room name may not be empty.";
		}
	}

	$template->set_filenames(array('body' => 'admin/rooms_body.tpl'));
	$template->assign_vars(array(
		'U_INDEX'	=> "index.$phpEx",
	));
	
	if ($notice != '') {
		$template->assign_block_vars('switch_notice', array('NOTICE' => $notice));
	}

	$sql	= "SELECT * FROM {$dbprefix}room_groups ORDER BY group_id";
	$res	= $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($res)) {
		$vars	= array();
		while (list($k, $v) = each($row)) {
			$vars[strtoupper($k)]	= $v;
		}
		$template->assign_block_vars('groups', $vars);

		$sql	= "SELECT *
				FROM
					{$dbprefix}rooms 
				WHERE
					room_group={$row['group_id']}
				ORDER BY
					room_name";
		$res2	= $db->sql_query($sql);
	
		while ($row2 = $db->sql_fetchrow($res2)) {
			$vars	= array();
			while (list($k, $v) = each($row2)) {
				$vars[strtoupper($k)]	= $v;
			}
			if ($row2['room_password'] != '') {
				$vars['PROTECTED']	= " [protected]";
			}
			if ($row2['room_limit'] != '0') {
				$vars['LIMITED']	= " [limit: {$row2['room_limit']}]";
			}
			$template->assign_block_vars('groups.rooms', $vars);
		}
	}
	$template->pparse('body');
} else if (($op == 'manage_groups') || ($op == 'dynamic_group') || ($op == 'new_group') || ($op == 'del_group')) {
	if ($op == 'dynamic_group') {
		$sql	= "UPDATE {$dbprefix}room_groups SET group_dynamic='no' WHERE group_id={$_REQUEST['old_group_id']}";
		$res	= $db->sql_query($sql);
		$sql	= "UPDATE {$dbprefix}room_groups SET group_dynamic='yes' WHERE group_id={$_REQUEST['group_id']}";
		$res	= $db->sql_query($sql);
		$notice	= "Updated the dynamic group.";
	} else if ($op == 'new_group') {
		if ($_REQUEST['group_name'] != '') {
			$sql	= "INSERT INTO {$dbprefix}room_groups (group_name) VALUES ('{$_REQUEST['group_name']}')";
			$res	= $db->sql_query($sql);
			$notice	= "Added group '{$_REQUEST['group_name']}'.";
		} else {
			$notice	= "<b>ERROR:</b> Group name may not be empty.";
		}
	} else if ($op == 'del_group') {
		$sql	= "SELECT * FROM {$dbprefix}room_groups WHERE group_id={$_REQUEST['gid']}";
		$res	= $db->sql_query($sql);
		$row	= $db->sql_fetchrow($res);
		$sql	= "DELETE FROM {$dbprefix}room_groups WHERE group_id={$_REQUEST['gid']}";
		$res	= $db->sql_query($sql);
		$notice	= "Removed group '{$row['group_name']}'.";
	}

	$template->set_filenames(array('body' => 'admin/groups_body.tpl'));
	$template->assign_vars(array(
		'U_INDEX'	=> "index.$phpEx"
	));

	if ($notice != '') {
		$template->assign_block_vars('switch_notice', array('NOTICE' => $notice));
	}

	$sql	= "SELECT * FROM {$dbprefix}room_groups ORDER BY group_id";
	$res	= $db->sql_query($sql);

	while ($row = $db->sql_fetchrow($res)) {
		$vars	= array();
		while (list($k, $v) = each($row)) {
			$vars[strtoupper($k)]	= $v;
		}
		if ($row['group_dynamic'] == 'yes') {
			$old_gid		= $row['group_id'];
			$vars['CHECKED']	= ' CHECKED';
		}
		$template->assign_block_vars('groups', $vars);
	}
	$template->assign_vars(array(
		'OLD_GROUP_ID'	=> $old_gid
	));
	$template->pparse('body');
} else if ($op == 'edit_room') {
	$sql	= "SELECT * FROM {$dbprefix}rooms WHERE room_id={$_REQUEST['rid']}";
	$res	= $db->sql_query($sql);
	$row	= $db->sql_fetchrow($res);
	
	print("<form method=post action=index.$phpEx>\n");
	print("<input type=hidden name=rid value={$_REQUEST['rid']}>\n");
	print("<input type=hidden name=op value=update_room>\n");
	print("<table class=row2 align=center width=70%>\n");
	print("<tr><th colspan=2>Edit Room</th></tr>\n");
	print("<tr><td>Room Name</td><td><input type=text name=room_name size=40 value=\"{$row['room_name']}\"></td></tr>\n");
	print("<tr><td>Room Limit</td><td><input type=text name=room_limit value=\"{$row['room_limit']}\"></td></tr>\n");
	print("<tr><td>Room Password</td><td><input type=text name=room_password value=\"{$row['room_password']}\"></td></tr>\n");
	print("<tr><td valign=top>Room Topic</td><td><textarea cols=80 rows=5 name=room_topic>{$row['room_topic']}</textarea></td></tr>\n");
	print("<tr><td>Room Moderating Group</td><td><select name=mod_group>");
	showModeratorGroups($row['mod_group']);
	print("</td></tr>\n");
	print("<tr><td colspan=2 align=center><input type=submit value='Update Room'></td></tr>\n");
	print("</table>\n");
} else if ($op == 'update_room') {

	$sql	= "UPDATE {$dbprefix}rooms
			SET
				room_name='{$_REQUEST['room_name']}',
				room_limit='{$_REQUEST['room_limit']}',
				room_password='{$_REQUEST['room_password']}',
				room_topic='{$_REQUEST['room_topic']}',
				mod_group='{$_REQUEST['mod_group']}'
			WHERE
				room_id={$_REQUEST['rid']}";
	$res	= $db->sql_query($sql);
	print("Updated {$_REQUEST['room_name']}.");
} else {
	print("Unknown operation '$op'.<br /><pre>");
	print_r($_REQUEST);
	print("</pre>");
}

print("</td></tr>");
print("</table>");
?>
